use strict;
use AppConfig qw(:expand :argcount);

our $delayederror;

our %access_data_conf = (
			# Multipost
			"multipost"				=> 0,   # Total number of identical messages (same md5) in the spool
		
			# Close connection instead of only rejecting an article when it exceeds more than dropratio times a limit
			"dropratio"				=> 0,

			# Per IP data
			"per_ip_total_articles"			=> 0,   # Total number of articles sent in the last spool_expire_time seconds
			"per_ip_total_size"			=> 0,	# Total bytes (accepted + rejected, headers + body)
                        "per_ip_total_bodysize" 		=> 0,   # Total number of bytes sent as body
                        "per_ip_total_headsize" 		=> 0,   # Total number of bytes sent as headers
			"per_ip_total_crosspost"                => 0,   # Total number of groups inside Newsgroups
			"per_ip_total_followups"                => 0,   # Total number of groups inside Followup-To
			"per_ip_accepted_articles" 		=> 0,   # Total number of accepted posts
			"per_ip_rejected_articles"		=> 0,	# Total number of rejected articles
			"per_ip_accepted_headsize"		=> 0,	# Total headers size for accepted articles
			"per_ip_rejected_headsize"		=> 0,	# Total headers size for rejected articles
			"per_ip_accepted_bodysize"		=> 0,   # Total body size for accepted articles
			"per_ip_rejected_bodysize"		=> 0,   # Total body size for rejected articles
			"per_ip_accepted_totalsize"		=> 0,   # Total size (headers+body) of accepted articles
			"per_ip_rejected_totalsize"		=> 0,   # Total size (headers+body) of rejected articles
			"per_ip_accepted_crosspost"		=> 0,   # Total number of groups inside Newsgroups (accepted articles only)
			"per_ip_rejected_crosspost"		=> 0,	# Total number of groups inside Newsgroups (rejected articles only)
			"per_ip_accepted_followups"		=> 0,	# Total number of groups inside Followup-To (accepted articles only)
			"per_ip_rejected_followups"		=> 0,	# Total number of groups inside Followup-To (rejected articles only)
                        "drop_per_ip_total_articles"                 => 0,   # Total number of articles sent in the last spool_expire_time seconds
                        "drop_per_ip_total_size"                     => 0,   # Total bytes (accepted + rejected, headers + body)
                        "drop_per_ip_total_bodysize"                 => 0,   # Total number of bytes sent as body
                        "drop_per_ip_total_headsize"                 => 0,   # Total number of bytes sent as headers
                        "drop_per_ip_total_crosspost"                => 0,   # Total number of groups inside Newsgroups
                        "drop_per_ip_total_followups"                => 0,   # Total number of groups inside Followup-To
                        "drop_per_ip_rejected_articles"              => 0,   # Total number of rejected articles
                        "drop_per_ip_rejected_headsize"              => 0,   # Total headers size for rejected articles
                        "drop_per_ip_rejected_bodysize"              => 0,   # Total body size for rejected articles
                        "drop_per_ip_rejected_totalsize"             => 0,   # Total size (headers+body) of rejected articles
                        "drop_per_ip_rejected_crosspost"             => 0,   # Total number of groups inside Newsgroups (rejected articles only)
                        "drop_per_ip_rejected_followups"             => 0,   # Total number of groups inside Followup-To (rejected articles only)

			# Per user data
			"per_user_total_articles"                 => 0,   # Total number of articles sent in the last spool_expire_time seconds
                        "per_user_total_bodysize"                 => 0,   # Total number of bytes sent as body
                        "per_user_total_headsize"                 => 0,   # Total number of bytes sent as headers
                        "per_user_total_size"                     => 0,   # Total bytes (accepted + rejected, headers + body)
                        "per_user_total_crosspost"                => 0,   # Total number of groups inside Newsgroups
                        "per_user_total_followups"                => 0,   # Total number of groups inside Followup-To
                        "per_user_accepted_articles"              => 0,   # Total number of accepted posts
                        "per_user_rejected_articles"              => 0,   # Total number of rejected articles
                        "per_user_accepted_headsize"              => 0,   # Total headers size for accepted articles
                        "per_user_rejected_headsize"              => 0,   # Total headers size for rejected articles
                        "per_user_accepted_bodysize"              => 0,   # Total body size for accepted articles
                        "per_user_rejected_bodysize"              => 0,   # Total body size for rejected articles
                        "per_user_accepted_totalsize"             => 0,   # Total size (headers+body) of accepted articles
                        "per_user_rejected_totalsize"             => 0,   # Total size (headers+body) of rejected articles
                        "per_user_accepted_crosspost"             => 0,   # Total number of groups inside Newsgroups (accepted articles only)
                        "per_user_rejected_crosspost"             => 0,   # Total number of groups inside Newsgroups (rejected articles only)
                        "per_user_accepted_followups"             => 0,   # Total number of groups inside Followup-To (accepted articles only)
                        "per_user_rejected_followups"             => 0,   # Total number of groups inside Followup-To (rejected articles only)
			"drop_per_user_total_articles"                 => 0,   # Total number of articles sent in the last spool_expire_time seconds
                        "drop_per_user_total_bodysize"                 => 0,   # Total number of bytes sent as body
                        "drop_per_user_total_headsize"                 => 0,   # Total number of bytes sent as headers
                        "drop_per_user_total_size"                     => 0,   # Total bytes (accepted + rejected, headers + body)
                        "drop_per_user_total_crosspost"                => 0,   # Total number of groups inside Newsgroups
                        "drop_per_user_total_followups"                => 0,   # Total number of groups inside Followup-To
                        "drop_per_user_rejected_articles"              => 0,   # Total number of rejected articles
                        "drop_per_user_rejected_headsize"              => 0,   # Total headers size for rejected articles
                        "drop_per_user_rejected_bodysize"              => 0,   # Total body size for rejected articles
                        "drop_per_user_rejected_totalsize"             => 0,   # Total size (headers+body) of rejected articles
                        "drop_per_user_rejected_crosspost"             => 0,   # Total number of groups inside Newsgroups (rejected articles only)
                        "drop_per_user_rejected_followups"             => 0,   # Total number of groups inside Followup-To (rejected articles only)


                        # Per domain data
                        "per_domain_total_articles"                 => 0,   # Total number of articles sent in the last spool_expire_time seconds
                        "per_domain_total_size"                     => 0,   # Total bytes (accepted + rejected, headers + body)
                        "per_domain_total_bodysize"                 => 0,   # Total number of bytes sent as body
                        "per_domain_total_headsize"                 => 0,   # Total number of bytes sent as headers
                        "per_domain_total_crosspost"                => 0,   # Total number of groups inside Newsgroups
                        "per_domain_total_followups"                => 0,   # Total number of groups inside Followup-To
                        "per_domain_accepted_articles"              => 0,   # Total number of accepted posts
                        "per_domain_rejected_articles"              => 0,   # Total number of rejected articles
                        "per_domain_accepted_headsize"              => 0,   # Total headers size for accepted articles
                        "per_domain_rejected_headsize"              => 0,   # Total headers size for rejected articles
                        "per_domain_accepted_bodysize"              => 0,   # Total body size for accepted articles
                        "per_domain_rejected_bodysize"              => 0,   # Total body size for rejected articles
                        "per_domain_accepted_totalsize"             => 0,   # Total size (headers+body) of accepted articles
                        "per_domain_rejected_totalsize"             => 0,   # Total size (headers+body) of rejected articles
                        "per_domain_accepted_crosspost"             => 0,   # Total number of groups inside Newsgroups (accepted articles only)
                        "per_domain_rejected_crosspost"             => 0,   # Total number of groups inside Newsgroups (rejected articles only)
                        "per_domain_accepted_followups"             => 0,   # Total number of groups inside Followup-To (accepted articles only)
                        "per_domain_rejected_followups"             => 0,   # Total number of groups inside Followup-To (rejected articles only)
 			"drop_per_domain_total_articles"                 => 0,   # Total number of articles sent in the last spool_expire_time seconds
                        "drop_per_domain_total_size"                     => 0,   # Total bytes (accepted + rejected, headers + body)
                        "drop_per_domain_total_bodysize"                 => 0,   # Total number of bytes sent as body
                        "drop_per_domain_total_headsize"                 => 0,   # Total number of bytes sent as headers
                        "drop_per_domain_total_crosspost"                => 0,   # Total number of groups inside Newsgroups
                        "drop_per_domain_total_followups"                => 0,   # Total number of groups inside Followup-To
                        "drop_per_domain_rejected_articles"              => 0,   # Total number of rejected articles
                        "drop_per_domain_rejected_headsize"              => 0,   # Total headers size for rejected articles
                        "drop_per_domain_rejected_bodysize"              => 0,   # Total body size for rejected articles
                        "drop_per_domain_rejected_totalsize"             => 0,   # Total size (headers+body) of rejected articles
                        "drop_per_domain_rejected_crosspost"             => 0,   # Total number of groups inside Newsgroups (rejected articles only)
                        "drop_per_domain_rejected_followups"             => 0,   # Total number of groups inside Followup-To (rejected articles only)

                        # Global data

                        "global_total_articles"                 => 0,   # Total number of articles sent in the last spool_expire_time seconds
                        "global_total_size"                     => 0,   # Total bytes (accepted + rejected, headers + body)
                        "global_total_bodysize"                 => 0,   # Total number of bytes sent as body
                        "global_total_headsize"                 => 0,   # Total number of bytes sent as headers
                        "global_total_crosspost"                => 0,   # Total number of groups inside Newsgroups
                        "global_total_followups"                => 0,   # Total number of groups inside Followup-To
                        "global_accepted_articles"              => 0,   # Total number of accepted posts
                        "global_rejected_articles"              => 0,   # Total number of rejected articles
                        "global_accepted_headsize"              => 0,   # Total headers size for accepted articles
                        "global_rejected_headsize"              => 0,   # Total headers size for rejected articles
                        "global_accepted_bodysize"              => 0,   # Total body size for accepted articles
                        "global_rejected_bodysize"              => 0,   # Total body size for rejected articles
                        "global_accepted_totalsize"             => 0,   # Total size (headers+body) of accepted articles
                        "global_rejected_totalsize"             => 0,   # Total size (headers+body) of rejected articles
                        "global_accepted_crosspost"             => 0,   # Total number of groups inside Newsgroups (accepted articles only)
                        "global_rejected_crosspost"             => 0,   # Total number of groups inside Newsgroups (rejected articles only)
                        "global_accepted_followups"             => 0,   # Total number of groups inside Followup-To (accepted articles only)
                        "global_rejected_followups"             => 0,   # Total number of groups inside Followup-To (rejected articles only)
                        "drop_global_total_articles"                 => 0,   # Total number of articles sent in the last spool_expire_time seconds
                        "drop_global_total_size"                     => 0,   # Total bytes (accepted + rejected, headers + body)
                        "drop_global_total_bodysize"                 => 0,   # Total number of bytes sent as body
                        "drop_global_total_headsize"                 => 0,   # Total number of bytes sent as headers
                        "drop_global_total_crosspost"                => 0,   # Total number of groups inside Newsgroups
                        "drop_global_total_followups"                => 0,   # Total number of groups inside Followup-To
                        "drop_global_rejected_articles"              => 0,   # Total number of rejected articles
                        "drop_global_rejected_headsize"              => 0,   # Total headers size for rejected articles
                        "drop_global_rejected_bodysize"              => 0,   # Total body size for rejected articles
                        "drop_global_rejected_totalsize"             => 0,   # Total size (headers+body) of rejected articles
                        "drop_global_rejected_crosspost"             => 0,   # Total number of groups inside Newsgroups (rejected articles only)
                        "drop_global_rejected_followups"             => 0,   # Total number of groups inside Followup-To (rejected articles only)

	);

my %access_bool_conf = (
                           );

my %access_list_conf = (

			  );

my %postfilter_bool_conf = (
				"posting_allowed"	=>     1,
				"spool_check"		=>     1,
				"style_check"		=>     1,
				"verbose"		=>     0,
			   );

my %postfilter_data_conf = (	"spool_file"		=> "/etc/news/filter/spool.log",   	# FILE where postfilter saves data about previously posted messages
				"active_file"		=> "/var/lib/news/active",	   	# active file handled by innd
				"two_level_tlds_file"   => "./modules/data/two-level-tlds",	# FILE which stores a list of all commonly used second level domain names
				"spool_expire_time"     => 86400				# expire time for data stored inside spool_file;
			   );


my %postfilter_list_conf = (
				"banned_group"			=> "",
				"banned_followup"		=> "",
			  );


our %style_data_conf = (
				"article_max_crosspost"		=>  0,			# Maximum number of groups inside Newsgroups
				"article_max_followup"		=>  0,			# Maximum number of groups inside Followup-To
				"article_max_headsize"		=>  0,			# Maximim number of bytes inside all headers
				"article_max_bodysize"		=>  0,			# Maximum size of body, in bytes
				"article_max_totalsize"		=>  0,			# Maximum overall size of each article
		
				"max_bodyline_length"   =>  0,		# Maximum length (in chars) of each body line				
				"max_empty_ratio"	=>  0,		# Maximum ratio of empty (^\n$) lines inside body
				"max_blank_ratio"	=>  0,		# Maximum ratio of blank (^[\t\ ]+$) lines inside body
				"max_quoted_ratio"	=>  0,		# Maximum ratio of quoted (^[\>|\:]+) lines inside body
				"max_email_ratio"	=>  0,		# Maximum ratio of lines which starts with an email address (^\w+\@\w+\.\w+)
				"min_welldone_ratio"    =>  0,		# Minimum ratio of body lines which are NOT empty, blank, quoted or which start with email addresses
				"max_article_age"	=>  0,		# Maximum number of seconds between arrival time (fourth field inside X-Trace) and date
				"max_future_age"	=>  0,		# Maximum number of seconds between a (future) date and arrival time

		      );

our %style_bool_conf = (
				"allow_all_control_messages"		=> 0,		# Allow all control messages without any check
			        "allow_control_cancel"			=> 0,		# Allow only control cancels
				"allow_also_control"			=> 0,		# Allow Also-Control header
				"allow_supersedes"			=> 0,		# Allow 'Cancel' 'Replaces' and 'Supersedes'
				"allow_approved_messages"   		=> 0,		# Allow users to add 'Approved' header
				"allow_external_followup"		=> 0,		# Whether is allowed a message with a group inside Followup-To that is not in Newsgroups
				"allow_re_without_references"   	=> 0,		# Allow a subject that starts with 'Re:' without a 'References' header
				"allow_uuencoded_data"			=> 0,		# Allow data encoded with UUEncode
				"allow_yenc_data"			=> 0,		# Allow data encoded with YENC
				"allow_posting_on_virgin_groups"	=> 0,		# Allow users to post inside groups that never received a message
				"allow_posting_on_empty_groups"		=> 0,		# Allow users to post inside empty groups
		      );

our %style_list_conf = (
				"banned_group"			=> "",		# Groups where posting is disabled
				"banned_followup"		=> "",		# Groups that can't be inserted inside Followup-To
		     );


sub load_config($$) {
	my $file = $_[0];
	my $code = $_[1];
	my (%input_data,%input_bool,%input_list);

	if ($code == 1) {      # postfilter.conf 
		%input_data = %postfilter_data_conf;
		%input_bool = %postfilter_bool_conf;	
		%input_list = %postfilter_list_conf;
	} elsif ($code == 2) { # User's rights
		%input_data = %access_data_conf;
		%input_bool = %access_bool_conf;
		%input_list = %access_list_conf;
	} elsif ($code == 3) { # Style
		%input_data = %style_data_conf;
		%input_bool = %style_bool_conf;
		%input_list = %style_list_conf;
	} else {
		&log("crit", "Unable to load config, unknown value $code inside load_config()");
		$delayederror = 1;
	}

	my $cfg = AppConfig->new( {ERROR => \&synterr} );

	&log("debug", "Loading $file");
	foreach (keys %input_data) {
                $cfg->define( "$_", {
                        DEFAULT  => "$input_data{$_}",
                        ARGCOUNT => ARGCOUNT_ONE,
                } );
        }

        foreach (keys %input_bool) {
                $cfg->define( "$_", {
                        DEFAULT  => "$input_bool{$_}",
                        ARGCOUNT => ARGCOUNT_NONE,
                } );
        }

        foreach (keys %input_list) {
                $cfg->define( "$_", {
                        ARGCOUNT => ARGCOUNT_LIST,
                } );
        }

        $cfg->file($file);
	return $cfg;
}


sub synterr() {
   my $error = $_[0];
   &log("crit", "Syntax error: $error");
   $delayederror = 1;
}

1;
